//截止时间选择事件
var opt1 = {
	preset: 'datetime',
	theme: 'ios',
	lang: 'zh',
	dateFormat: "yy年mm月dd日"
};
$('#sel_time').mobiscroll(opt1);

//附赠条件开关事件
$(".extra-cost-list .switch").change(function() {
	var that = $(this);
	var index = that.attr("data-index");
	var itemPrice = that.attr("data-price");
	var singleCost = $(".single-cost input");
	var lastPrice = singleCost.attr("data-price");
	var currPrice;
	if(that.is(":checked")) {
		/*选中*/
		currPrice = (lastPrice * 1 + itemPrice * 1);
	} else {
		/*取消选中*/
		currPrice = (lastPrice * 1 - itemPrice * 1);
		//		singleCost.attr("placeholder","不少于"+currPrice+"推币");
		//		singleCost.attr("data-price",currPrice);
	}
	singleCost.attr("placeholder", "不少于" + currPrice + "推币");
	singleCost.attr("data-price", currPrice);

	that.parents("li").find(".toggle-input").slideToggle();
	$(".cost-list li").eq(index).slideToggle();

	var totalPrice = $(".total-cost span");
	//	totalPrice.attr("data-single-price", currPrice);
	totalPrice.attr("data-total-price", currPrice);
	totalPrice.text(currPrice + "推币");
});

//限定任务量
$(".cost-count .switch").change(function() {
	var that = $(this);
	if(!that.is(":checked")) {
		that.parents(".cost-count").find("input").val("");
		var totalPrice = $(".total-cost span");
		var singlePrice = totalPrice.attr("data-total-price");
		console.log(singlePrice)
		totalPrice.text(singlePrice + "推币");
	} else {
		
	}
	that.parents(".cost-count").find(".toggle-input").slideToggle();
});
//输入数量
$(".cost-count .toggle-input input").on("change", function() {
	var counts = $(this).val();
	if(counts > 0) {
		var totalPrice = $(".total-cost span");
		var lastTotalPrice = totalPrice.attr("data-total-price");
		var currTotalPric = (counts * 1) * (lastTotalPrice * 1);
		totalPrice.text(currTotalPric + "推币");
	}

});

//下载额外功能
$(".download_fun_list .switch").change(function() {
	var that = $(this);
	var index = that.attr("data-index");
	if(!that.is(":checked")) {
		that.parents("li").find("input[type=number]").val("");
	}
	that.parents("h4").find(".item-cost").fadeToggle();
	that.parents("li").find(".item-fun-require").slideToggle();

	$(".cost-list li").eq(index).slideToggle();
});

//下载 单选条目
$(".require-check-item .check").change(function() {
	var that = $(this);
	if(that.is(":checked")) {}
	that.parents(".require-check-item").find(".toggle-item").fadeToggle();
});
//图片缩放
//function scaleRefresh(dom) {
//	ImagesZoom.init({
//		"elem": dom,
//		"delBack": function(index) {
//
//		}
//	});
//}

//上传图片
var uploadImages = function(uploadInputId) {
	var input = $(uploadInputId); //获取选择图片的input元素
	var contInput = input.parent();
	var showImg = contInput.find(".show-img"); //获取显示图片的div元素
	//这边是判断本浏览器是否支持这个API。
	if(typeof FileReader === 'undefined') {
		showImg.html("抱歉，该程序暂不不支持 FileReader");
		input.attr('disabled', 'disabled');
	} else {
		input.on('change', readFile)
	}

	function readFile() {
		var file = this.files[0];
		//判断file的类型是不是图片类型。
		if(!/image\/\w+/.test(file.type)) {
			alert("文件必须为图片！");
			return false;
		}

		var reader = new FileReader(); //声明一个FileReader实例
		reader.readAsDataURL(file); //调用readAsDataURL方法来读取选中的图像文件
		reader.onloadstart = function(e) {
			myLayer.load("加载中...");
		};
		//最后在onload事件中，获取到成功读取的文件内容，并以插入一个img节点的方式显示选中的图片
		reader.onload = function(e) {
			myLayer.clear();
			var imgBase64 = this.result;
			if(uploadInputId == "#upload_btn_screen") {
				showImg.html('<img src="' + imgBase64 + '" alt=""/>');
			} else {
				showImg.html('<img src="' + imgBase64 + '" alt=""/><a class="c-f28 c-fe2b51 ml-20" id="del_img_btn">删除</a>');
			}
			showImg.show();
			contInput.hide();
			input.remove();
			scaleRefresh(".show-img");

			showImg.find("#del_img_btn").click(function() {
				showImg.html("");
				contInput.append('<input type="file" name="" id="upload_btn" capture="*" accept="image/*" />')
				contInput.show();
				uploadImages("#upload_btn");
				showImg.hide();
			});
		}

	}
}